package com.parsifal.client;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.google.gson.Gson;
import com.parsifal.client.bean.ServerConfig;
import com.parsifal.client.callback.SocketStateCallback;
import com.parsifal.client.socket.Client;

/**
 * Created by yangming on 17-5-17.
 */
public class ClientActivity extends AppCompatActivity {

    private static final String TAG = ClientActivity.class.getSimpleName();

    private ListView mListView = null;

    private EditText mEditText = null;

    private Button mButton = null;

    private ClientAdapter mAdapter = null;

    private Client mClient = null;

    private Handler mHandler = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHandler = new Handler();
        mListView = (ListView) findViewById(R.id.lv_content);
        mEditText = (EditText) findViewById(R.id.et_send_str);
        mButton = (Button) findViewById(R.id.btn_send);
        mAdapter = new ClientAdapter(this);
        mListView.setAdapter(mAdapter);

        String ip;
        int port;
        String json = getIntent().getStringExtra(QRActivity.MSG);
        Log.e(TAG, "json " + json);
        if (null != json) {
            ServerConfig serverConfig = new Gson().fromJson(json, ServerConfig.class);
            if (null != serverConfig) {
                ip = serverConfig.ip;
                port = serverConfig.port;
                Log.e(TAG, "ip " + ip);
                Log.e(TAG, "port " + port);
            } else {
                return;
            }
        } else {
            return;
        }
        mClient = new Client();
        mClient.setIP(ip);
        mClient.setPort(port);
        mClient.setSocketStateCallback(new SocketStateCallback() {
            @Override
            public void onConnected() {
                Log.e(TAG, "onConnected");
            }

            @Override
            public void onDisconnected() {
                Log.e(TAG, "onDisconnected");
            }

            @Override
            public void onReceive(final String msg) {
                Log.w(TAG, "Receive " + msg);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.addServerMessage(msg);
                        mListView.setSelection(mAdapter.getCount() - 1);
                    }
                });
            }

            @Override
            public void onSend(final String msg) {
                Log.e(TAG, "Send " + msg);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.addClientMessage(msg);
                        mListView.setSelection(mAdapter.getCount() - 1);
                    }
                });
            }
        });
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = mEditText.getText().toString();
                mClient.send(msg);
            }
        });
        mClient.connect();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mClient.stopClient();
    }
}
